package com.lsh.oms.core.service.business.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.api.model.status.CancelOrderDto;
import com.lsh.oms.core.enums.OrderOperateType;
import com.lsh.oms.core.enums.OrderStatus;
import com.lsh.oms.core.model.http.HttpMessage;
import com.lsh.oms.core.model.order.OrderHead;
import com.lsh.oms.core.proxy.service.BaseProxy;
import com.lsh.oms.core.proxy.service.TmsProxy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

/**
 * @author panxudong
 * @date 19/3/23
 */
@Slf4j
public abstract class AbstractCancelOrderMisService extends AbstractCancelOrderService {

    @Autowired
    private TmsProxy tmsProxy;

    @Override
    protected boolean checkParams(CancelOrderDto cancelOrderDto) {
        boolean flag = false;
        Long orderCode = cancelOrderDto.getOrderCode();
        if (super.checkOrderCode(orderCode)) {
            flag = true;
        }

        return flag;
    }

    @Override
    protected boolean checkStatus(int status, int region) {
        boolean flag = false;
        if (status < OrderStatus.ORDER_SORTING.getIndex()) {
            if (status != OrderStatus.ORDER_COMMITED_OFC.getIndex()) {
                flag = true;
            }
        }
        return flag;
    }

    @Override
    protected CommonResult<Boolean> cancel(OrderHead orderHead, String logSign) {
        Long orderCode = orderHead.getOrderCode();
        int status = orderHead.getOrderStatus().intValue();
        CommonResult<Boolean> result = this.execute(orderHead, logSign);
        if (!result.getData()) {
            return result;
        }

        Integer type = 1;
        JSONObject ext = JSON.parseObject(orderHead.getExt());
        Integer sale_model = ext.getInteger("saleModel");
        Integer delivery_type = ext.getInteger("deliveryType");

        if(null != delivery_type){
            if (delivery_type == 3) {
            type = 4;
            }else if(delivery_type == 4){
                return CommonResult.success(true);
            }
        }

        if (this.isRequestTms(status)) {
            boolean isSuccessForRequestTms = false;
            try {
                HttpMessage httpMessage = this.tmsProxy.cancelTask(logSign, orderCode, type, orderHead.getVenderId());
                isSuccessForRequestTms = BaseProxy.isSuccess(httpMessage.getMessage());
            } catch (IOException e) {
                log.error(orderHead.getOrderCode() + " 订单取消tms 失败", e);
            }

            if (!isSuccessForRequestTms) {
                return CommonResult.error(null, false);
            }
        }

        return CommonResult.success(true);
    }

    @Override
    protected OrderOperateType getOperateType() {
        return OrderOperateType.ORDER_CANCEL_BY_CUSTOM_SERVICE;
    }

    @Override
    protected OrderStatus getOrderStatus() {
        return OrderStatus.ORDER_CANCEL_CUSTOMERSERVICE;
    }

    private boolean isRequestTms(int status) {
        boolean flag = false;
        if (status >= OrderStatus.ORDER_WM_NEW.getIndex() && status <= OrderStatus.ORDER_WM_NEW.getIndex()) {
            flag = true;
        }
        return flag;
    }

    protected abstract CommonResult<Boolean> execute(OrderHead orderHead, String logSign);
}
